package com.yx.backstage.controller;

import com.yx.backstage.config.global.CommonResult;
import com.yx.backstage.manager.SysUserManager;
import com.yx.backstage.model.param.SysUserParam;
import com.yx.backstage.model.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author YangXiong
 */
@Slf4j
@Api(tags = "用户管理控制器")
@RequestMapping("/sys-user")
@RestController
public class SysUserController {
    @Resource
    private SysUserManager sysUserManager;

    @GetMapping("/detail")
    @ApiOperation("根据用户名获取用户详情")
    public CommonResult<SysUserVo> getUserByName(String name) {
        log.info("SysUserController info getUserByName userName={}", name);
        return sysUserManager.getUserByName(name);
    }

    @PostMapping("/page")
    @ApiOperation("获取管理员列表")
    @PreAuthorize("@ex.hasAuthority('admin')")
    public CommonResult<List<SysUserVo>> page(@RequestBody SysUserParam param){
        return sysUserManager.page(param);
    }

    @PostMapping("/user/list")
    @ApiOperation("分页查询普通用户列表")
    public CommonResult<List<SysUserVo>> getUserList(@RequestBody SysUserParam param){
        return sysUserManager.getUserList(param);
    }

    @PostMapping("save")
    @ApiOperation("新增管理员")
    @PreAuthorize("@ex.hasAuthority('admin')")
    public CommonResult<String> save(@RequestBody SysUserParam param){
        return sysUserManager.save(param);
    }

    @PostMapping("/update/status")
    @ApiOperation("启用或者禁用用户")
    public CommonResult<Boolean> updateStatus(@RequestBody SysUserParam param) {
        log.info("SysUserController updateStatus info SysUserParam={}", param);
        Boolean result = sysUserManager.updateStatus(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("启用或者禁用用户失败");
    }

    @PostMapping("/delete/user")
    @ApiOperation("删除普通用户")
    public CommonResult<Boolean> deleteUser(@RequestBody SysUserParam param) {
        log.info("SysUserController deleteUser info SysUserParam={}", param);
        Boolean result = sysUserManager.deleteUser(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("删除失败");
    }

    @PostMapping("/delete/manager")
    @PreAuthorize("@ex.hasAuthority('admin')")
    @ApiOperation("删除管理员")
    public CommonResult<Boolean> deleteManager(@RequestBody SysUserParam param) {
        log.info("SysUserController deleteUser info SysUserParam={}", param);
        Boolean result = sysUserManager.deleteUser(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("删除失败");
    }

    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    public CommonResult<Map<String,String>> updateUser(@RequestBody SysUserParam user){
        return sysUserManager.updateUser(user);
    }
}
